#include "main.h"
#include <stm32f0xx.h>
/*variables---------------------*/
uint32_t x;
uint32_t y;
volatile uint16_t State_state=0x15;
/* Set DAC value, DAC_OUT1 = (3.3 * DAC_value) / 4095 and 	*/
uint16_t DAC_value = 800;
//double reference = 0.4;

uint32_t Frequency=300000;
volatile uint16_t adc_reading;
//Phase_address: 
uint16_t Phase_state[4]={0x2A,0x22,0x15,0x11};
volatile uint16_t Phase_index=3;
int a;

int main(void)
{
	// Initialize PWM output pins
	STM_EVAL_LEDInit(LED3);	
	// DAC
	DAC_config();
	//DAC_value= (uint16_t) reference*4095/3.3;
	DAC_SetChannel1Data(DAC_Align_12b_R, DAC_value);
	
		// PWM DAC
	//PWM_DAC_start();
	
	Phase_init();
 //COMPARATOR
	//comp_init();

  //GPIOB->ODR = 0x15;
	PHASE_TIM15_start();
	//FREQUENCY_TIM2_start();
//	STM_EVAL_LEDOn(LED3);
	// ADC
	//adc_init();
	//ADC_StartOfConversion(ADC1);
	
	//PHI_init(PHI3);
	//GPIOB->ODR = 0x2a;
	
	//#define hysteresis
	#ifndef hysteresis
	while(1)
	{
	}
	#endif
	
//	while(1){
//		for (x=0;x<1;x++)
//		{
//		}
//		Phase_state ^= 0x3F;
//		//GPIOB->ODR = 0x00;;
//		GPIOB->ODR = Phase_state;
//	}
	while(1){


		for (a=0;a<500000;a++)
		{
		}

	//STM_EVAL_LEDToggle(LED3);		
}
	


//	while(1);
//	return(0);	
}


void change_Frequency()
{
	uint16_t TimerPeriod;
	Frequency=Frequency-20000;
	if (Frequency<160000)
		Frequency=700000;
	TimerPeriod = (SystemCoreClock / Frequency ) - 1;
	TIM15->ARR =TimerPeriod;
}